package elitech.vietnam.myfashion.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.extend.ViewPagerAdapter;

public class MainPageView extends LinearLayout {

	private MainActivity mContext;
	public ViewPagerAdapter mPageAdapter;
	public ViewPager mViewPager;
	public ImageButton mMenuButton;
	
	public MainPageView(MainActivity context) {
		super(context);
		this.mContext = context;
		mContext.mMainPageView = this;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (inflater != null) {
			inflater.inflate(R.layout.mainviewpage, this);
		}

		mViewPager = (ViewPager) this.findViewById(R.id.main_pager);
		mMenuButton = (ImageButton) findViewById(R.id.main_btnMenu);
		
		mViewPager.setOffscreenPageLimit(3);
		mPageAdapter = new ViewPagerAdapter(mContext);
		mViewPager.setAdapter(mPageAdapter);
		
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int pos) {
				if (mContext.mUser != null)
					switch (pos) {
					case 0:
//						mContext.changeTitle(mContext.getString(R.string.bestoftoday), false);
						if(mPageAdapter.mBestGridView!=null)
							mPageAdapter.mBestGridView.GetProduct();
						break;
					case 1:
//						mContext.changeTitle(mContext.getString(R.string.categories), false);
						if(mPageAdapter.mCategoriesView != null){
							mPageAdapter.mCategoriesView.GetCategories();
						}
						break;
					case 2:
//						mContext.changeTitle(mContext.getString(R.string.myshopping), false);
						if(mContext.mMyShoppingView!=null){
							if(mContext.mMyShoppingView.mBuyingView!=null && mContext.mUser != null){
								mContext.mMyShoppingView.mBuyingView.refreshCart();
								mContext.mMyShoppingView.mBuyingView.GetOrder();
							}
							if(mContext.mMyShoppingView.mFavorView!=null && mContext.mUser != null)
								mContext.mMyShoppingView.mFavorView.RefreshFavorite();
						}
						break;
					case 3:
//						mContext.changeTitle(mContext.getString(R.string.styler), false);
						if(mPageAdapter.mStylerView != null){
							if(mPageAdapter.mStylerView.mAllStyleView != null)
								mPageAdapter.mStylerView.mAllStyleView.RefreshAllStyle();
							if(mPageAdapter.mStylerView.mFollowStyleView != null)
								mPageAdapter.mStylerView.mFollowStyleView.RefreshFollowStyle();
						}
						break;
					case 4:
//						mContext.changeTitle(mContext.getString(R.string.mystyle), false);
						if(mContext.mEditedInfo)
							mPageAdapter.mMyStyleView.LoadMemberInfo();
						if(mPageAdapter.mMyStyleView != null)
							mPageAdapter.mMyStyleView.RefreshMyStyle();
						break;
					case 5:
//						mContext.changeTitle(mContext.getString(R.string.setting), false);
						break;
					}
				else
					switch (pos) {
					case 0:
//						mContext.changeTitle(mContext.getString(R.string.bestoftoday), false);
						if(mPageAdapter.mBestGridView!=null)
							mPageAdapter.mBestGridView.GetProduct();
						break;
					case 1:
//						mContext.changeTitle(mContext.getString(R.string.categories), false);
						if(mPageAdapter.mCategoriesView != null){
							mPageAdapter.mCategoriesView.GetCategories();
						}
						break;
					case 2:
//						mContext.changeTitle(mContext.getString(R.string.styler), false);
						if(mPageAdapter.mStylerView != null){
							if(mPageAdapter.mStylerView.mAllStyleView != null)
								mPageAdapter.mStylerView.mAllStyleView.RefreshAllStyle();
						}
						break;
					case 3:
//						mContext.changeTitle(mContext.getString(R.string.setting), false);
						break;
					}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});

		mMenuButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mContext.callMenu();
			}
		});
	}
	
}
